package com.hmbe.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmbe.dto.Result;
import com.hmbe.dto.UserDTO;
import com.hmbe.entity.Blog;
import com.hmbe.service.IBlogService;
import com.hmbe.service.IUserService;
import com.hmbe.utils.SystemConstants;
import com.hmbe.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private IBlogService blogService;
    @Resource
    private IUserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;

    // BlogController  根据id查询博主的探店笔记
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /**
     * 保存笔记
     *
     * @param blog 笔记
     * @return 笔记id
     */
    @PostMapping
    public Result saveBlog(@RequestBody Blog blog) {

        // 保存探店博文
        return blogService.saveBlog(blog);

    }

    /** 根据id查询博客详情
     *
     * @param id 博客id
     * @return blog
     */
    @GetMapping("/{id}")
    public Result queryBlog(@PathVariable("id") Long id) {

        // 根据id查询
        return blogService.queryBlogById(id);


    }


    /**
     * 根据笔记id点赞该笔记
     *
     * @param id 笔记id
     * @return blog
     */

    @PutMapping("/like/{id}")
    public Result likeBlog(@PathVariable("id") Long id) {
        // 修改点赞数量
        /*blogService.update()
                .setSql("liked = liked + 1").eq("id", id).update();*/
        blogService.likeBlogById(id);
        return Result.ok();
    }

    /**
     * 分页查询我的笔记列表
     *
     * @param current 当前页
     * @return list
     */
    @GetMapping("/of/me")
    public Result queryMyBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        // 查询用户

        return Result.ok(records);
    }

    /**
     * 分页查询笔记列表
     *
     * @param current 当前页
     * @return list
     */
    @GetMapping("/hot")
    public Result queryHotBlog(@RequestParam(value = "current", defaultValue = "1") Integer current) {
        return blogService.queryHotBlog(current);
    }
    /*
     * 查询笔记点赞数量
     *
     * @param id 博客id
     * @return blog
     */
    @GetMapping("/likes/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id) {

        return blogService.queryBlogLikes(id);
    }
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(
            @RequestParam("lastId") Long max, @RequestParam(value = "offset", defaultValue = "0") Integer offset){
        return blogService.queryBlogOfFollow(max, offset);
    }
}
